class Solution {
public:
    void dfs(int targerSum, int k, int sum, vector<int> &path, vector<vector<int>> &result, int ind) {
        if (sum > targerSum) return ;
        if (path.size() > k) return ;
        if (k == path.size() && sum == targerSum) {
            result.push_back(path);
            return ;
        }
        for (int i = ind; i <= 9 - (k - path.size()) + 1; i++) {
            sum += i;
            path.push_back(i);
            dfs(targerSum, k, sum, path, result, i + 1);
            sum -= i;
            path.pop_back();
        }
        return ;
    }
    vector<vector<int>> combinationSum3(int k, int n) {
        vector<int> path;
        vector<vector<int>> result;
        dfs(n, k, 0, path, result, 1);
        return result;
    }
};
